package com.yidu.hbats.action.TrainingPlan;
import com.yidu.hbats.action.vo.AtsPlanCurriculumvo;
import com.yidu.hbats.model.*;
import com.yidu.hbats.service.TrainingPlan.AtsEmpAthleteSchemeService;
import com.yidu.hbats.service.atsBase.AtsBaseSportService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanCurriculumService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanItemService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanWeekService;
import com.yidu.hbats.service.emp.AtsEmpCoachService;
import com.yidu.utils.AtsPlanItemTree;
import com.yidu.utils.FileTools;
import com.yidu.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张韦超
 * @类的描述——
 * @date 2019/10/30下午 02:29
 */
@Controller
@RequestMapping("atsPlanCurriculumController")
public class AtsPlanCurriculumController {
    @Resource
    AtsPlanCurriculumService atsPlanCurriculumService;

    //训练指标
    @Resource
    AtsPlanItemService atsPlanItemService;
    @Resource
    AtsPlanWeekService atsPlanWeekService;
    //教练表的注入
    @Resource
    AtsEmpCoachService atsEmpCoachService;
    //体育训练表的注入
    @Resource
    AtsBaseSportService atsBaseSportService;
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;
    //文件上传管理工具
    @Resource
    FileTools fileTools;

    /**
     *
     * @param atsPlanItem
     * @return
     */
    @RequestMapping("curEchar")
    @ResponseBody
    public List<AtsPlanItemTree> echar(AtsPlanItem atsPlanItem) {
        List<AtsPlanItemTree> treeList = atsPlanItemService.curEchar(atsPlanItem);
        return treeList;

    }

    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String, Object> selectAll(HttpServletRequest request,AtsPlanCurriculum atsPlanCurriculum, @RequestParam(required = false, defaultValue = "1", value = "page") Integer pageNum, @RequestParam(required = false, defaultValue = "10", value = "limit") Integer pageSize) {

        int count = atsPlanCurriculumService.count(request,atsPlanCurriculum);
        List<AtsPlanCurriculum> list = atsPlanCurriculumService.selectAll(request,atsPlanCurriculum, pageNum, pageSize);

        Map<String, Object> map = new HashMap<>();

        map.put("data", list);

        map.put("count", count);

        map.put("code", 0);

        map.put("msg", "");//消息

        return map;
    }




    @RequestMapping("updateIsva")
    @ResponseBody
    public Message updateIsva(AtsPlanCurriculum atsPlanCurriculum) {
        int rows = atsPlanCurriculumService.updateIsva(atsPlanCurriculum);
        //删除计划同时删除下面指标
        AtsPlanItem atsPlanItem=new AtsPlanItem();
        atsPlanItem.setIsva(0);
        atsPlanItem.setPid(atsPlanCurriculum.getCurriculumId());
        //删除对应的运动员
        AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
        atsEmpAthleteScheme.setIsva(0);
        atsEmpAthleteScheme.setAthleteId(atsPlanCurriculum.getCurriculumId());
        Message mes = new Message();
        if (rows >= 1) {
            atsEmpAthleteSchemeService.updatePidFive(atsEmpAthleteScheme);
            atsPlanItemService.updateOrdeletefive(atsPlanItem);
            mes.setNum(1);
            mes.setMsg("删除成功");
        } else {
            mes.setNum(0);
            mes.setMsg("删除失败");
        }


        return mes;
    }

    @RequestMapping("delAll")
    @ResponseBody
    public Message delAll(String id) {
        int rows = atsPlanCurriculumService.delAll(id);
        Message mes = new Message();
        if (rows >= 1) {
            mes.setNum(1);
            mes.setMsg("修改成功");
        } else {
            mes.setNum(0);
            mes.setMsg("修改失败");
        }


        return mes;
    }

    @RequestMapping("findById")
    @ResponseBody
    public Map<String, Object> findById(AtsPlanItem atsPlanItem, AtsPlanCurriculum atsPlanCurriculum) {
        Map<String, Object> map = atsPlanItemService.curSelectPid(atsPlanItem, atsPlanCurriculum);
        return map;
    }

    @RequestMapping("addOrupdate")
    @ResponseBody
    public Message addOrupdate(AtsPlanCurriculumvo atsPlanCurriculumvo, HttpSession session) {
        atsPlanCurriculumvo.setRemark("1");
        atsPlanCurriculumvo.setIsva(1);
        //根据下拉框的教练id查出教练名
        AtsEmpCoach atsEmpCoach = atsEmpCoachService.findById(atsPlanCurriculumvo.getCoachId());
        //根剧下拉框每周id查询
        AtsPlanWeek atsPlanWeek = atsPlanWeekService.selectByPrimaryKey(atsPlanCurriculumvo.getWeekId());
        //传入体育项目名
        atsPlanCurriculumvo.setItemName(atsPlanCurriculumvo.getSportName());
        //传入教练名
        atsPlanCurriculumvo.setCoachName(atsEmpCoach.getCoachName());
        //传入每周名字
        atsPlanCurriculumvo.setWeekName(atsPlanWeek.getWeekName());
        Message message = new Message();

        //ManagerModel managerModel= (ManagerModel) session.getAttribute("manager");
        //取到当前登录人
        //managerModel.getManUser();
        //atsPlanCurriculumvo.setCreater("admin");
        //给指标传创建人
        //atsPlanYearMorevo.setCreateName("");
        if (atsPlanCurriculumvo.getCurriculumId() != null && !"".equals(atsPlanCurriculumvo.getCurriculumId())) {
            atsPlanCurriculumService.updateIsva(atsPlanCurriculumvo);
        } else {
            atsPlanCurriculumService.insertSelective(atsPlanCurriculumvo);
        }
        //传成功
        message.setNum(1);
        message.setMsg("操作成功!");
        return message;
    }

    @RequestMapping("findAll")
    @ResponseBody
    public AtsPlanCurriculum findById(Integer curriculumId) {
        AtsPlanCurriculum atsPlanWeek = atsPlanCurriculumService.findByIdTwo(curriculumId);
        return atsPlanWeek;
    }

    /**
     * 查询当前最大的id
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("findMaxPid")
    public AtsPlanCurriculum findMaxPid(int coachId) {
        AtsPlanCurriculum atsPlanWeek = atsPlanCurriculumService.findMaxPid(coachId);
        return atsPlanWeek;
    }

    @ResponseBody
    @RequestMapping("findPlanItemYearMore")
    public List<AtsPlanItem> findPlanItemYearMore(Integer pid) {
        List<AtsPlanItem> list = atsPlanItemService.findPlanItemYearMorefive(pid);
        return list;
    }

    @ResponseBody
    @RequestMapping("findPlanItemYearMoreone")
    public List<AtsPlanItem> findPlanItemYearMoreone(Integer pid) {
        List<AtsPlanItem> list = atsPlanItemService.findPlanItemYearMorefive(pid);
        return list;
    }

    @ResponseBody
    @RequestMapping("deleteZhibiao")
    public Message deleteZhibiao(Integer itemId) {
        AtsPlanItem atsPlanItem = new AtsPlanItem();
        atsPlanItem.setIsva(0);
        atsPlanItem.setItemId(itemId);
        Message message = new Message();
        int rows = atsPlanItemService.updateByPrimaryKeySelectivefive(atsPlanItem);
        if (rows > 0) {
            message.setMsg("删除成功!");
            message.setNum(1);
        } else {
            message.setMsg("删除失败!");
            message.setNum(0);
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("findDivone")
    public List<AtsPlanItem> findDivone() {
        List<AtsPlanItem> list = atsPlanItemService.findDivfive();
        return list;
    }
}
